package com.example.project.projectspringcloud;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.dao.sys.SysConfigMapper;
import com.example.project.projectspringcloud.dao.video.VideoMapper;
import com.example.project.projectspringcloud.entity.sys.SysConfig;
import com.example.project.projectspringcloud.entity.video.Video;
import com.example.project.projectspringcloud.utils.RedisUtil;
import com.example.project.projectspringcloud.utils.TencentVideoUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ScheduleTester {

    private static final Logger logger = LoggerFactory.getLogger(ScheduleTester.class);

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Resource
    private RedisUtil redisUtil;

    @Test
    public void videoExpireCheckTest() throws Exception {
        List<SysConfig> sysConfigList = sysConfigMapper.selectAll(new SysConfig("tencentcloud"));
        Set<Integer> videoIds = videoMapper.selectAllVideoUrl(new Video());
        videoIds.forEach(t -> {
            if (!redisUtil.hasKey(Constants.VIDEO_CACHE_PREFIX + t)) {
                Video video = videoMapper.selectOne(new Video(t));
                try {
                    redisUtil.set(Constants.VIDEO_CACHE_PREFIX + video.getId(), TencentVideoUtil.getTCPlayParam(video, false, sysConfigList, redisUtil, false), 86400);
                } catch (Exception e) {
                    logger.error("videoExpireCheck", e);
                }
            }
        });
    }
}
